package com.hansheng.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Field;
import java.util.List;


/**
 * Created by GoldenMan on 2016/12/19.
 */
public class JsonHelper {
    public static String GET_SESSION_ERROR = "Session获取失败";
    public static String USER_INFO_ERROR = "无法获取用户信息，请重试！";
    public static String NULL_RESULT_ERROR = "无法获取相关信息，请核对后重试！";
    public static String OPER_FAILED = "操作失败，请重试！";
    public static String LOGIN_FAILED = "登陆失败，请重试！";
    public static String REGISTER_WXUSER_FAILED = "注册微信用户失败，请重试！";
    public static String REGISTER_USER_FAILED = "注册用户失败，请重试！";
    public static String SEND_PIC_FAILED = "发送验证码失败，请重试！";
    public static String ADD_SKU_2_CART_FAILED = "发送验证码失败，请重试！";
    public static String COUNT_ERROR = "商品库存不足，请修改数量后重试！";
    public static String PAYED_ERROR = "订单已支付，请勿重复提交！";

    /**
     * getReturnJSON改名为JSONBuilder
     *
     * @param msg
     * @param data
     * @return
     */
    public static JSONObject JSONBuilder(int msg, JSONObject data) {
        JSONObject returnValue = new JSONObject();
        returnValue.put("data", data);
        returnValue.put("msg", msg);
        return returnValue;
    }

    public static JSONObject JSONBuilder(int msg, JSONArray data) {
        JSONObject returnValue = new JSONObject();
        returnValue.put("data", data);
        returnValue.put("msg", msg);
        return returnValue;
    }

    public static JSONObject listJSONBuilder(int msg, JSONObject returnValue) {
        returnValue.put("msg", msg);
        return returnValue;
    }

    public static JSONObject successJSONBuilder() {
        JSONObject returnValue = new JSONObject();
        returnValue.put("data", "SUCCESS");
        returnValue.put("msg", 0);
        return returnValue;
    }

    public static JSONObject successJSONBuilder(JSONArray jsonArray) {
        JSONObject returnValue = new JSONObject();
        returnValue.put("data", jsonArray);
        returnValue.put("msg", 0);
        return returnValue;
    }

    public static JSONObject successJSONBuilder(JSONObject jsonObject) {
        JSONObject returnValue = new JSONObject();
        returnValue.put("data", jsonObject);
        returnValue.put("msg", 0);
        return returnValue;
    }

    public static JSONObject errJSONBulider(int errCode, String errMsg) {
        JSONObject returnValue = new JSONObject();
        JSONObject errJson = new JSONObject();
        errJson.put("errMsg", errMsg);
        returnValue.put("data", errJson);
        returnValue.put("msg", errCode);
        return returnValue;
    }

    public static JSONArray list2JSONArray(List list) {
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            Field fields[] = list.get(i).getClass().getDeclaredFields();
            try {
                Field.setAccessible(fields, true);
                JSONObject tempJson = new JSONObject();
                for (Field field : fields) {
                    tempJson.put(field.getName(), field.get(list.get(i)));
                }
                jsonArray.add(tempJson);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return jsonArray;
    }

    public static JSONObject object2JSONObject(Object o) {
        JSONObject jsonObject = new JSONObject(true);
        Field fields[] = o.getClass().getDeclaredFields();
        try {
            Field.setAccessible(fields, true);
            for (Field field : fields) {
                jsonObject.put(field.getName(), field.get(o));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
}
